﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CommunitySystem.JinShengService;
using System.IO;

namespace CommunitySystem.PageView
{
    public partial class SelJSWindow : ChildWindow
    {
        private readonly JinShengServiceClient _client = new JinShengServiceClient();
        private JinShen_MainInfo_Attatchment fafile = null;
        public SelJSWindow(JinShen_MainInfoLSJL jsls,string title)
        {
            InitializeComponent();
            this.Title = title+"信息";

            if (title == "方案审核")
            {
                spfa.Visibility = Visibility.Visible;
                spry.Visibility = Visibility.Collapsed;
            }
            else if (title == "人员审核")
            {
                spfa.Visibility = Visibility.Collapsed;
                spry.Visibility = Visibility.Visible;
            }
            if (jsls.Status == "审核不通过" && jsls.jsfile.Count!=0)
            {
                fafile = jsls.jsfile.First();
                spfafile.Visibility = Visibility.Visible;
                tbrealname.Content = jsls.jsfile.First().realname;
            }
                
            this.DataContext = jsls;
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
        /// <summary>
        /// 方案下载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        SaveFileDialog openFile;
        private List<byte> fileContent = new List<byte>();
        private void tbrealname_Click(object sender, RoutedEventArgs e)
        {
            SilverlightMessageBoxLibrary.CustomMessage cm = new SilverlightMessageBoxLibrary.CustomMessage("确认要下载此方案吗?", SilverlightMessageBoxLibrary.CustomMessage.MessageType.Confirm, null);
            cm.Show();
            cm.OKButton.Click += (sender1, e1) =>
            {
                string extend = fafile.realname.Substring(fafile.realname.LastIndexOf('.'));
                openFile = new SaveFileDialog() { Filter = string.Format("{0}|*.{0}", extend) };
                if (openFile.ShowDialog() == true)
                {
                    fileContent.Clear();
                 
                    IsWorking.Visibility = Visibility.Visible;
                    busy.BusyContent = "正在下载数据，请稍后...";

                    _client.DownLoadFile1Completed -= new EventHandler<JinShengService.DownLoadFile1CompletedEventArgs>(_client_DownLoadFile1Completed);
                    _client.DownLoadFile1Completed += new EventHandler<JinShengService.DownLoadFile1CompletedEventArgs>(_client_DownLoadFile1Completed);

                    _client.DownLoadFile1Async(fafile.filename);
                }

            };
        }
        void _client_DownLoadFile1Completed(object sender, JinShengService.DownLoadFile1CompletedEventArgs e)
        {
            Stream myStream;
            if ((myStream = openFile.OpenFile()) != null)
            {
                using (BinaryWriter bw = new BinaryWriter(myStream))
                {

                    bw.Write(e.Result);
                    myStream.Dispose();
                    myStream.Close();
                    SilverlightMessageBoxLibrary.Message.InfoMessage("下载完毕!");
                    IsWorking.Visibility = Visibility.Collapsed;
                }

            }
        }
    }
}

